﻿var app = angular.module('stuApp', ['ngResource','ngCookies']);
app.controller('stuCtr', function ($rootScope,$scope, mainService, $cookieStore) {
    $scope.chapterList = [];
    
    //选择章节
    $scope.chooseChapter = function (outIndex, index) {
        $scope.chapterid = $scope.chapterList[outIndex].chaptree[index].id;
        if (outIndex > 0 && index == 0 && $scope.chapterList[outIndex - 1].chaptree[$scope.chapterList[outIndex - 1].chaptree.length - 1].scores < 0.6) {
            $("#warning").modal('show');  
        }
        else if (index > 0 && $scope.chapterList[outIndex].chaptree[index-1].scores < 0.6) {
            $("#warning").modal('show');  
        }
        else {
            $scope.page_jump();
         }
    }
    //跳转闯关页面
    $scope.page_jump = function () {
        $cookieStore.put("again_type", 0);
        window.location.href = "/modules/student/tmpl/pass.html?chapterid=" + $scope.chapterid;
    }
    
    //章节列表
    $scope.getChapter = function () {
        $scope.par_chapter = {
            "empid": $cookieStore.get('loginInfo').empid,
            "materialid": $rootScope.material_id,
        };
        mainService.getChapter($scope.par_chapter, function (data) {
            $scope.chapterList = data.data;
            for (var i = 0; i < $scope.chapterList.length; i++) {
                for (var j = 0; j < $scope.chapterList[i].chaptree.length; j++) {
                    if ($scope.chapterList[i].chaptree[j].scores > 0.6) {
                        $scope.chapterList[i].chaptree[j].islock = false;
                    }else if (i > 0 && j == 0 && $scope.chapterList[i - 1].chaptree[$scope.chapterList[i - 1].chaptree.length - 1].scores > 0.6) {
                        $scope.chapterList[i].chaptree[j].islock = false;
                    } else if (j > 0 && $scope.chapterList[i].chaptree[j - 1].scores > 0.6) {
                        $scope.chapterList[i].chaptree[j].islock = false;
                    } else {
                        $scope.chapterList[i].chaptree[j].islock = true;
                    }
                }
            }
        })
    }

    //获取学生选择的教材
    $scope.Getstu_selmater = function () {
        var par = {
            "student_id": $cookieStore.get('loginInfo').empid,
        }
        mainService.Getstu_selmater(par, function (data) {
            $rootScope.material_id = data.data[0].material_id;
            $scope.getChapter();
        })
    }
    //加载
    $scope.init_data = function () {
        if ($rootScope.material_id == null || $rootScope.material_id.length <= 0) {
            $scope.Getstu_selmater();
        } else {
            $scope.getChapter();
        }
    }
    $scope.init_data();
    })